<template>
  <div id="home">
    <!--  导航栏  -->
    <nav-bar class="home-nav">
      <template #center>
        购物车
      </template>
    </nav-bar>
    <tab-control :title="title" @tabClick="tabClickOn" ref="tabControl1" class="tab-control" v-show="isTabShow"/>
    <!-- 滚动 -->
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll" :pull-up-load="true" @pulling="Upload">
      <!--  轮播图  -->
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
      <!--  分类   -->
      <recommend-view :recommends="recommends"/>
      <!--  图片  -->
      <feature-view/>
      <!--  第二导航栏  -->
      <tab-control :title="title" @tabClick="tabClickOn" ref="tabControl2"/>
      <!--  商品列表  -->
      <goods-list :goods="showTab" :type-arr="indexType"/>
    </scroll>
    <back-top @click="backClick" v-show="isShow"/>
  </div>
</template>

<script>
import {itemListMixin, backTop} from "@/common/mixin";
import {getHomeMultidata, getHomeGoods} from "@/network/Home";

import HomeSwiper from "@/views/home/childComps/HomeSwiper";
import RecommendView from "@/views/home/childComps/RecommendView";
import FeatureView from "@/views/home/childComps/FeatureView";

import NavBar from "@/components/common/navbar/NavBar";
import TabControl from "@/components/common/tabControl/TabControl";
import GoodsList from "@/components/content/goods/GoodsList";
import Scroll from "@/components/common/scroll/Scroll";

export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
  },
  mixins: [itemListMixin, backTop],
  data() {
    return {
      banners: [],
      recommends: [],
      title: ['流行', '精选', '新品'],
      goods: {
        'pop': {page: 0, list: []},
        'new': {page: 0, list: []},
        'sell': {page: 0, list: []},
      },
      indexType: 'pop',
      tabOffsetTop: 0,
      isTabShow: false,
      saveY: 0
    }
  },
  computed: {
    showTab() {
      return this.goods[this.indexType].list
    }
  },
  mounted() {
    /* mixin(混入)将定义的内容传入对应的模块中,类似继承,将相同的代码放到一起 */
  },
  created() {
    this.getHomeMultidata()
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')
  },
  methods: {
    /**
     * 事件监听
     */
    tabClickOn(index) {
      switch (index) {
        case 0:
          this.indexType = 'pop'
          break
        case 1:
          this.indexType = 'new'
          break
        case 2:
          this.indexType = 'sell'
          break
      }
      this.$refs.tabControl1.count = index
      this.$refs.tabControl2.count = index

    },
    contentScroll(pro) {
      this.isShow = pro.y < -1000
      this.isTabShow = pro.y < -this.tabOffsetTop
    },
    Upload() {
      this.getHomeGoods(this.indexType)
      // this.$refs.scroll.scroll.refresh()
    },
    swiperImageLoad() {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
    },
    /**
     * 网络请求
     */
    getHomeMultidata() {
      getHomeMultidata()
        .then(res => {
          this.banners = res.data.data.banner.list
          this.recommends = res.data.data.recommend.list
        })
        .catch(err => {
          console.log('请求失败')
          console.log(err)
        })
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1
      getHomeGoods(type, page)
        .then(res => {
          this.goods[type].list.push(...res.data.data.list)
          this.goods[type].page += 1
          /* 第二次加载 */
          this.$refs.scroll.finishPullUp()
        })
        .catch(err => {
        })
    }
  },
  activated() {
    this.$refs.scroll.scrollToUp(0, this.saveY)
    this.$refs.scroll.scroll.refresh()
  },
  deactivated() {
    this.saveY = this.$refs.scroll.scroll.y
  }
}
</script>

<style scoped>
#home {
  /*padding-top: 44px;*/
  height: 100vh;
  position: relative;
}

.home-nav {
  background-color: var(--color-tint);
  color: white;

  /*position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 9;*/
}

.tab-control {
  position: relative;
  z-index: 9;
}

.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}

/* 方案一 */
/*.content{
  height: calc(100% - 93px);
  overflow: hidden;
  margin-top: 44px;
}*/

</style>
